package com.five.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.admin.dto.BrandQueryDto;
import com.five.admin.server.BrandService;
import com.five.core.result.Result;
import com.five.core.result.ResultEnum;
import com.five.pojo.dto.BatchIds;
import com.five.pojo.entity.Brand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping(value = "/brand")
@Slf4j
public class BrandController {
    @Resource
    private BrandService brandService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result<?> list(@RequestBody @Valid BrandQueryDto queryDto){
        Page<Brand> page = new Page<>(queryDto.getPage(), queryDto.getLimit());
        Page<Brand> resultPage = brandService.findAll(page, queryDto);
        return Result.buildSuccess(resultPage.getTotal(), resultPage.getRecords());
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result<?> add(@RequestBody @Valid Brand brand){
        if (brandService.save(brand)) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.ADD_FALSE);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result<?> update(@RequestBody @Valid Brand brand){
        if (brandService.updateById(brand)) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.UPDATE_FALSE);
    }

    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public Result<?> deleteById(@RequestBody @Valid Long  id){
        if (brandService.removeById(id)) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.DELETE_FALSE);
    }

    @RequestMapping(value = "/deleteByIds",method = RequestMethod.POST)
    public Result<?> deleteByIds(@RequestBody BatchIds ids){
        if (brandService.removeByIds(ids.getIds())) {
            return Result.buildSuccess();
        }
        return Result.buildFail(ResultEnum.DELETE_FALSE);
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Result<?> getAll(){
        return Result.buildSuccess(brandService.list());
    }
}
